home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / hsrc_117.zip / GETPUT.C < prev    next >
Text File  |  1990-09-16  |  6KB  |  247 lines

  1. /* HeadEdit miscellaneous get/put text & header functions */
  2.  
  3. #include "msgg.h"
  4. #include "twindow.h"
  5. #include "keys.h"
  6. #include "headedit.h"
  7.  
  8. extern WINDOW *ewnd;
  9.  
  10.  
  11. char * pascal get_text () {
  12.  
  13.  char once;
  14.  static char *hold;
  15.  char *tempo;
  16.  word temp=0;
  17.  int  handle;
  18.  char reduced=0;
  19.  
  20.  sprintf(filename,"%sXTEXT.%03x",path,areano);
  21.  while ((handle=_open(filename,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  22.      if (errno==EACCES) {
  23.         temp++;
  24.         if (temp>2) {
  25.             error_message(" Can't get access ");
  26.             return NULL;
  27.         }
  28.         any_message(" Awaiting access ");
  29.         sleep(1);
  30.      }
  31.      else {
  32.         error_message(" Can't open datafile ");
  33.         return NULL;
  34.      }
  35.  }
  36.  temp=msg.length;
  37.  once=0;
  38. TryThatAgain:
  39.  if (lseek(handle,msg.start,SEEK_SET)==-1) {
  40.      if (eof(handle)==-1) {
  41.         if (!once) {
  42.             once++;
  43.             goto TryThatAgain;
  44.         }
  45.         else perror (" TEXT SEEK ERROR");
  46.         _close(handle);
  47.         return NULL;
  48.      }
  49.  }
  50. ReTry:
  51.   hold=(char *)malloc(msg.length+1);
  52.   if (hold==NULL) {
  53.     if (msg.length>1024 && !(msg.m_attr & MSGPACKED)) {
  54.         msg.length-=256;
  55.         any_message(" Reducing text size to fit... ");
  56.         reduced=1;
  57.         goto ReTry;
  58.     }
  59.     else {
  60.         _close(handle);
  61.         return NULL;
  62.     }
  63.   }
  64.   memset(hold,0,msg.length);
  65.   if(_read(handle,hold,msg.length)<msg.length) reduced=1;
  66.   _close(handle);
  67.   hold[msg.length-1]=0;
  68.   hold[msg.length]=0;
  69.   tempo=hold;
  70.  
  71.   if(!(msg.m_attr & MSGTREATED) && !(currarea->attr & ANSI) &&!(msg.m_attr & MSGPACKED)) {
  72.       while (*tempo) {
  73.         if(*tempo=='\x8d' || *tempo=='\n') {
  74.             if(*tempo=='\n') memmove(tempo,&tempo[1],msg.length-((word)tempo-(word)hold));
  75.             else {
  76.                 if(*(tempo-1)==' ' && tempo>hold) memmove(tempo,&tempo[1],msg.length-((word)tempo-(word)hold));
  77.                 else {
  78.                     *tempo=' ';
  79.                     tempo++;
  80.                 }
  81.             }
  82.         }
  83.         else tempo++;
  84.       }
  85.       if(*(tempo-1)!='\r' && tempo>hold) {
  86.         *tempo='\r';
  87.         tempo[1]=0;
  88.       }
  89.       if(!reduced && temp>((word)strlen(hold)+1) && *hold) {
  90.         msg.length=(word)strlen(hold)+1;
  91.         sprintf(filename,"%sXTEXT.%03x",path,areano);
  92.         if ((handle=_open(filename,O_RDWR | O_BINARY | O_DENYWRITE))==-1) goto OkayThenDont;
  93.         once=0;
  94. TryThatAgain2:
  95.         if (lseek(handle,msg.start,SEEK_SET)==-1) {
  96.          if (eof(handle)==-1) {
  97.             if (!once) {
  98.                 once++;
  99.                 goto TryThatAgain;
  100.             }
  101.             else {
  102.                 _close(handle);
  103.                 goto OkayThenDont;
  104.             }
  105.          }
  106.         }
  107.         if(_write(handle,hold,(word)strlen(hold))==(word)strlen(hold)) {
  108.             _write(handle,"\0",(word)sizeof(char));
  109.             if(eof(handle)==-1) {
  110.                 _close(handle);
  111.                 goto OkayThenDont;
  112.             }
  113.             _close(handle);
  114.             msg.m_attr=(msg.m_attr | MSGTREATED);
  115.             put_mess();
  116.             temp=msg.length;
  117.         }
  118.       }
  119.   }
  120.  
  121.   if(msg.m_attr & MSGPACKED && !reduced) {
  122.        if(unpack_msg(&hold)==NULL) {
  123.             if(hold) free(hold);
  124.             msg.length=0;
  125. /* printf("\x1b[KCame back NULL: hold=%p",hold);    */
  126.             return NULL;
  127.        }
  128. /*       printf("\x1b[KCame back ok: hold=%p",hold);    */
  129.   }
  130.  
  131. OkayThenDont:
  132.   msg.length=temp;
  133.   return hold;
  134. }
  135.  
  136.  
  137.  
  138. word pascal put_text (char far *hold,char *extra,char *extra1,struct ffblk *f,char *origintext) {
  139.  
  140.  char once;
  141.  word messlen=0;
  142.  int handle;
  143.  
  144.  if (hold==NULL || !*hold) return 0;
  145.  sprintf(filename,"%sXTEXT.%03x",path,areano);
  146.  if (findfirst(filename,f,0)) f->ff_fsize=0;
  147.  
  148.  if ((handle=_open(filename,O_RDWR | O_BINARY | O_APPEND | O_DENYWRITE))==-1) {
  149.      if((handle=creat(filename,S_IWRITE))==-1) {
  150.          if (errno==EACCES) error_message(" Access temporarily denied ");
  151.      }
  152.      else error_message(" Can't open text database ");
  153.      pause();
  154.      return 0;
  155.  }
  156.  once=0;
  157. TryThatAgain:
  158.  if (lseek(handle,0,SEEK_END)==-1) {
  159.      if (eof(handle)==-1) {
  160.         if (!once) {
  161.             once++;
  162.             goto TryThatAgain;
  163.         }
  164.         else {
  165.             error_message (" Error finding EOF ");
  166.             _close(handle);
  167.             pause();
  168.             return 0;
  169.         }
  170.      }
  171.  }
  172.   if (*extra && extra) messlen=_write(handle,extra,sizeof(char)*strlen(extra));
  173.   if (*extra1 && extra1) messlen+=_write(handle,extra1,sizeof(char)*strlen(extra1));
  174.   messlen+=_write(handle,hold,sizeof(char)*strlen(hold));
  175.   if(*origintext && origintext) messlen+=_write(handle,origintext,sizeof(char)*strlen(origintext));
  176.   _write(handle,"\0",1);
  177.   messlen+=2;
  178.   _close(handle);
  179.   return (messlen);
  180. }
  181.  
  182.  
  183.  
  184. void pascal get_mess (char which) {
  185.  
  186.  int handle;
  187.  struct _xmsg *ptr;
  188.  
  189.  if(!nomess) return;
  190.  sprintf(filename,"%sXDATA.%03x",path,areano);
  191.  
  192.  if (messno>nomess || messno<1) messno=1;
  193.  if ((handle=_open(filename,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  194.      if (errno==EACCES) error_message("Access temporarily denied.");
  195.      else error_message("Can't open msg datafile");
  196.      return;
  197.  }
  198.  ptr = which ? &msg2 : &msg;
  199.  if ((lseek(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET)==-1) || (_read(handle,ptr,sizeof(struct _xmsg))<1)) {
  200.     if (eof(handle)==-1) perror ("\nSEEK ERROR");
  201.  }
  202.  _close(handle);
  203. }
  204.  
  205.  
  206.  
  207.  
  208. void pascal put_mess () {
  209.  
  210.  int handle;
  211.  register word x=0;
  212.  
  213.  if (messno>nomess || messno<1) return;
  214.  sprintf(filename,"%sXDATA.%03x",path,areano);
  215.  
  216.  while ((handle=_open(filename,O_RDWR | O_BINARY | O_DENYNONE))==-1) {
  217.      if (errno==EACCES) {
  218.         x++;
  219.         if(x>2) {
  220.             error_message(" Got tired of waiting... ");
  221.             pause();
  222.             return;
  223.         }
  224.         any_message(" Awaiting access... ");
  225.         sleep(1);
  226.      }
  227.      else {
  228.         if((handle=creat(filename,S_IWRITE))==-1) {
  229.             error_message(" Cannot open message file... ");
  230.             return;
  231.         }
  232.         break;
  233.      }
  234.  }
  235.  clear_message();
  236.  lock(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),(long)sizeof(struct _xmsg));
  237.  if ((lseek(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET)==(-1)) || (_write(handle,&msg,sizeof(struct _xmsg))<1)) {
  238.     if (eof(handle)==(-1))  {
  239.         error_message(" Seek error! ");
  240.         pause();
  241.     }
  242.  }
  243.  unlock(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),(long)sizeof(struct _xmsg));
  244.  _close(handle);
  245. }
  246.  
  247.